/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.controller.dashboard;

import br.com.jr2tecnologia.ConstantsOfSystem;
import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.SMSMailUtil;
import br.com.jr2tecnologia.model.CarrierPersistenceLocal;
import br.com.jr2tecnologia.model.CompanyPersistenceLocal;
import br.com.jr2tecnologia.model.LoginPersistenceLocal;
import br.com.jr2tecnologia.model.ModemPersistenceLocal;
import br.com.jr2tecnologia.model.entity.Carrier;
import br.com.jr2tecnologia.model.entity.Company;
import br.com.jr2tecnologia.model.entity.Login;
import br.com.jr2tecnologia.model.entity.Modem;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author ronildo
 */

@ManagedBean
@SessionScoped
public class EditModemMBean implements Serializable{

    private static final Logger logger = LoggerFactory.getLogger();

    @EJB
    private CompanyPersistenceLocal companyPersistence;
    @EJB
    private LoginPersistenceLocal loginPersistence;
    @EJB
    private CarrierPersistenceLocal carrierPersistence;
    @EJB
    private ModemPersistenceLocal modemPersistence;

    private String updateCompanySelected;
    private String updateLoginSelected;
    private String updateCarrierSelected;

    private List<Company> listCompany;
    private List<Login> listLogin;
    private List<Carrier> listCarrier;

    private Modem selectedModem;

    public EditModemMBean(){
        logger.info("loading EditModemMBean");
        selectedModem = new Modem(new Company(), new Login(), new Carrier());
    }

    public Modem getSelectedModem() {
        return selectedModem;
    }

    public void setSelectedModem(Modem selectedModem) {
        this.selectedModem = selectedModem;
        editModem();
    }

    public String getUpdateCarrierSelected() {
        return updateCarrierSelected;
    }

    public void setUpdateCarrierSelected(String updateCarrierSelected) {
        this.updateCarrierSelected = updateCarrierSelected;
    }

    public String getUpdateCompanySelected() {
        return updateCompanySelected;
    }

    public void setUpdateCompanySelected(String updateCompanySelected) {
        this.updateCompanySelected = updateCompanySelected;
    }

    public String getUpdateLoginSelected() {
        return updateLoginSelected;
    }

    public void setUpdateLoginSelected(String updateLoginSelected) {
        this.updateLoginSelected = updateLoginSelected;
    }

    public List<SelectItem> getMenuCompany() {
        List<SelectItem> menuCompany = new ArrayList<SelectItem>();
        listCompany = companyPersistence.listAll();

        menuCompany.add(new SelectItem(0, ""));
        for(int cont = 1, index = 0; index < listCompany.size(); cont++, index++){
            menuCompany.add(new SelectItem(cont, listCompany.get(index).getName()));
        }
        return menuCompany;
    }

    public List<SelectItem> getMenuLogin() {
        List<SelectItem> menuLogin = new ArrayList<SelectItem>();
        listLogin = loginPersistence.listAll();

        menuLogin.add(new SelectItem(0, ""));
        for(int cont = 1, index = 0; index < listLogin.size(); cont++, index++){
            menuLogin.add(new SelectItem(cont, listLogin.get(index).getEmail()));
        }
        return menuLogin;
    }

    public List<SelectItem> getMenuCarrier() {
        List<SelectItem> menuCarrier = new ArrayList<SelectItem>();
        listCarrier = carrierPersistence.list();

        menuCarrier.add(new SelectItem(0, ""));
        for(int cont = 1, index = 0; index < listCarrier.size(); cont++, index++){
            menuCarrier.add(new SelectItem(cont, listCarrier.get(index).getName()));
        }
        return menuCarrier;
    }

    public void editModem(){
        listCompany = companyPersistence.listAll();
        listLogin = loginPersistence.listAll();
        listCarrier = carrierPersistence.list();

        for(int index = 0; index < listCompany.size(); index++){
            long pk_company = listCompany.get(index).getPkCompany().longValue();
            Company company = selectedModem.getCompany();
            if(company != null && company.getPkCompany() != null && pk_company == company.getPkCompany().longValue()){
                updateCompanySelected = String.valueOf(++index);
                break;
            }
        }

        for(int index = 0; index < listLogin.size(); index++){
            long pk_login = listLogin.get(index).getPkLogin().longValue();
            Login login = selectedModem.getLogin();
            if(login != null && login.getPkLogin() != null && pk_login == login.getPkLogin().longValue()){
                updateLoginSelected = String.valueOf(++index);
                break;
            }
        }

        for(int index = 0; index < listCarrier.size(); index++){
            long pk_carrier = listCarrier.get(index).getPkCarrier().longValue();
            Carrier carrier = selectedModem.getCarrier();
            if(carrier != null && carrier.getPkCarrier() != null && pk_carrier == carrier.getPkCarrier().longValue()){
                updateCarrierSelected = String.valueOf(++index);
                break;
            }
        }
    }

    public void update(){
        if(Integer.parseInt(updateCompanySelected) > 0)
            selectedModem.setCompany(listCompany.get(Integer.parseInt(updateCompanySelected)-1));
        else
            selectedModem.setCompany(null);

        if(Integer.parseInt(updateLoginSelected) > 0)
            selectedModem.setLogin(listLogin.get(Integer.parseInt(updateLoginSelected)-1));
        else
            selectedModem.setLogin(null);

        if(Integer.parseInt(updateCarrierSelected) > 0)
            selectedModem.setCarrier(listCarrier.get(Integer.parseInt(updateCarrierSelected)-1));
        else
            selectedModem.setCarrier(null);

        modemPersistence.update(selectedModem);
        SMSMailUtil.putFacesMessage("Sucesso", "Modem editado com sucesso");
        ConfigDashboardMBean configMBean = (ConfigDashboardMBean)SMSMailUtil.getAttributeInSessionMap(ConstantsOfSystem.CONFIGMBEAN);
        configMBean.searchModem();
    }
}